#include "include.h"

gboolean draw_callback(GtkWidget *widget, cairo_t *cr, gpointer data)
{
	// 绘制每个粒子
	for (int i = 0; i < par_num; i++)
	{
		cairo_set_source_rgb(cr, particles[i].R, particles[i].G, particles[i].B); // 设置绘制颜色
		cairo_arc(cr, particles[i].x, particles[i].y, PAR_R, 0, 2 * G_PI);		  // 绘制圆形粒子
		cairo_fill(cr);															  // 填充粒子
	}
#ifdef SHOW_Gravi_Line
	struct regional *p;
	p = &regionals[0][0];

	calculate_regional_acc();
	cairo_set_line_width(cr, 1.0); // 设置线宽

	cairo_set_source_rgb(cr, 1, 0, 0); // 设置线条颜色
	cairo_move_to(cr, 0, 0);
	cairo_line_to(cr, 0, WIN_H);
	cairo_stroke(cr);
	cairo_move_to(cr, 0, 0);
	cairo_line_to(cr, WIN_W, 0);
	cairo_stroke(cr);
	cairo_move_to(cr, WIN_W, WIN_H);
	cairo_line_to(cr, WIN_W, 0);
	cairo_stroke(cr);
	cairo_move_to(cr, WIN_W, WIN_H);
	cairo_line_to(cr, 0, WIN_H);
	cairo_stroke(cr);

	cairo_set_source_rgb(cr, 0, 1, 0); // 设置线条颜色
	for (int x = 0; x < WIN_W; x += GridRatio)
	{
		cairo_move_to(cr, x, 0);
		cairo_line_to(cr, x, WIN_H);
		cairo_stroke(cr);
	}
	for (int y = 0; y < WIN_H; y += GridRatio)
	{
		cairo_move_to(cr, 0, y);
		cairo_line_to(cr, WIN_W, y);
		cairo_stroke(cr);
	}

	cairo_set_source_rgb(cr, 0, 0, 0); // 设置线条颜色
	for (int i = 0; i < sizeof(regionals) / sizeof(struct regional); i++)
	{
		int ix, iy;
		iy = i / REGIONAL_X;
		ix = i % REGIONAL_X;
		ix *= GridRatio;
		iy *= GridRatio;
		ix += GridRatio / 2;
		iy += GridRatio / 2;
		cairo_move_to(cr, ix - 2, iy - 2);
		cairo_line_to(cr, ix - p[i].ax, iy - p[i].ay);
		cairo_stroke(cr);
		cairo_move_to(cr, ix - 2, iy + 2);
		cairo_line_to(cr, ix - p[i].ax, iy - p[i].ay);
		cairo_stroke(cr);
		cairo_move_to(cr, ix + 2, iy - 2);
		cairo_line_to(cr, ix - p[i].ax, iy - p[i].ay);
		cairo_stroke(cr);
		cairo_move_to(cr, ix + 2, iy + 2);
		cairo_line_to(cr, ix - p[i].ax, iy - p[i].ay);
		cairo_stroke(cr);
	}
#endif
	return FALSE;
}

gboolean update_particles(gpointer data)
{
	// 重新绘制窗口
	gtk_widget_queue_draw(drawing_area);

	return G_SOURCE_CONTINUE;
}

// 键盘输入事件处理函数
gboolean on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
{
	switch (event->keyval)
	{
	case GDK_KEY_Up: // 键盘输入加速度
		ya_g -= 10;
		break;
	case GDK_KEY_Down: // 键盘输入加速度
		ya_g += 10;
		break;
	case GDK_KEY_Left: // 键盘输入加速度
		xa_g -= 10;
		break;
	case GDK_KEY_Right: // 键盘输入加速度
		xa_g += 10;
		break;
	case GDK_KEY_c: // 键盘输入退出
		exit(0);
		break;
	case GDK_KEY_r: // 键盘输入复位
		par_init();
		break;

	default:
		break;
	}
	// g_print("xa_g += %02.3f ,ya_g += %02.3f ,Key pressed: %s\n", xa_g, ya_g, gdk_keyval_name(event->keyval));
	return TRUE;
}

// 鼠标点击事件处理函数
gboolean on_button_press(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
	// 鼠标点击哪里 就在哪里创建25个球
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			particles[par_num].x = event->x + (i - 2) * 10; // 随机初始化x坐标
			particles[par_num].y = event->y + (j - 2) * 10; // 随机初始化y坐标
			particles[par_num].vx = 0;						// 初始化x方向速度
			particles[par_num].vy = 0;						// 初始化y方向速度
			particles[par_num].vx = 0;						// 初始化x方向速度
			particles[par_num].vy = 0;						// 初始化y方向速度
			particles[par_num].ax = 0;						// 初始化x方向加速度
			particles[par_num].ay = 0;						// 初始化y方向加速度
			particles[par_num].R = (rand() % 256) / 256.0f; // 随机初始化颜色R
			particles[par_num].G = (rand() % 256) / 256.0f; // 随机初始化颜色G
			particles[par_num].B = (rand() % 256) / 256.0f; // 随机初始化颜色B
			par_num++;
		}
	}
	return TRUE;
}

void gtk_ui_init(int argc, char *argv[])
{
	GtkWidget *window; // 声明GTK窗口变量
	guint timer_id_1;
	guint timer_id_2;

	gtk_init(&argc, &argv);													 // 初始化GTK
	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);							 // 创建顶层窗口
	gtk_window_set_default_size(GTK_WINDOW(window), WIN_W + 10, WIN_H + 10); // 设置窗口大小

	drawing_area = gtk_drawing_area_new();											   // 创建绘图区域
	gtk_container_add(GTK_CONTAINER(window), drawing_area);							   // 将绘图区域添加到窗口中
	g_signal_connect(G_OBJECT(drawing_area), "draw", G_CALLBACK(draw_callback), NULL); // 连接绘图信号

	// 连接键盘和鼠标事件信号
	g_signal_connect(window, "key-press-event", G_CALLBACK(on_key_press), NULL);
	g_signal_connect(window, "button-press-event", G_CALLBACK(on_button_press), NULL);

	gtk_widget_show_all(window); // 显示窗口中的所有部件

	timer_id_1 = g_timeout_add(10, update_particles, NULL); // 创建定时器,每隔10毫秒更新粒子位置并重新绘制窗口
	timer_id_2 = g_timeout_add(1000, update_fps, NULL);		// 计算fps用
}